<?php

/**
 * This is the model class for table "host_login".
 *
 * The followings are the available columns in table 'host_login':
 * @property integer $id_host_login
 * @property string $id_host
 * @property integer $protocol
 * @property string $request_uri
 * @property integer $method
 * @property string $field_username
 * @property string $field_password
 * @property string $username
 * @property string $password
 * @property string $password_secure
 * @property string $extra_variables
 */
class HostLogin extends CActiveRecord
{
	const PROTOCOL_HTTP = 1;
	const PROTOCOL_HTTPS = 2;
	const METHOD_POST = 1;
	const METHOD_GET = 2;
	
	public $EnP = true;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return HostLogin the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'host_login';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('protocol, method, id_host, request_uri, field_username, field_password, username, password', 'required'),
			array('protocol, method', 'numerical', 'integerOnly'=>true),
			array('id_host, request_uri, field_username, field_password, username, password, password_secure, extra_variables', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_host_login, id_host, protocol, request_uri, method, field_username, field_password, username, password, extra_variables', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_host_login' => 'Id Host Login',
			'id_host' => 'Id Host',
			'protocol' => 'Protocol',
			'request_uri' => 'Request Uri',
			'method' => 'Method',
			'field_username' => 'Field Username',
			'field_password' => 'Field Password',
			'username' => 'Username',
			'password' => 'Password',
			'extra_variables' => 'Extra Variables',
		);
	}

	public function beforeSave() {
//		if ($this->isNewRecord){
			$this->passwordEncode();
//		}
		return true;
	}
	
	public function passwordEncode()
	{
		if(!empty($this->password)){
			if($this->EnP){
				$crypt = new Crypt();
				$this->password = $crypt->encrypt($this->password);
				$this->password_secure = $crypt->getKey();
			}
		}
	}
	
	public function passwordDecode()
	{
		$crypt = new Crypt();
		return $crypt->decrypt($this->password, $this->password_secure);
	}
	
	public function getPlainPassord()
	{
		if(empty($this->password_secure)){
			return $this->password;
		}else{
			return $this->passwordDecode();
		}
	}
	
	public function __get($key)
	{
		$action = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
		if(method_exists($this, $action)){
			return $this->$action();
		}
		
		return parent::__get($key);
	}
	
	public function getPlainMethod()
	{
		return ($this->method == self::METHOD_GET)? 'get' : 'post';
	}
	
	public function getPlainProtocol()
	{
		return ($this->protocol == self::PROTOCOL_HTTP)? 'http' : 'https';
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_host_login',$this->id_host_login);
		$criteria->compare('id_host',$this->id_host,true);
		$criteria->compare('protocol',$this->protocol);
		$criteria->compare('request_uri',$this->request_uri,true);
		$criteria->compare('method',$this->method);
		$criteria->compare('field_username',$this->field_username,true);
		$criteria->compare('field_password',$this->field_password,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('extra_variables',$this->extra_variables,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function listProtocolOptions()
	{
		return array(
			self::PROTOCOL_HTTP => 'Protocol: http://',
			self::PROTOCOL_HTTPS => 'Protocol: https://'
		);
	}
	
	public function listMethodOptions()
	{
		return array(
			self::METHOD_POST => 'Method: POST',
			self::METHOD_GET => 'Method: GET',
		);
	}
	
	public function makeUpLink()
	{
		$parsedUrl = explode('?', "{$this->plainProtocol}://{$this->request_uri}");
		$url = $parsedUrl[0];
		$params = array();
		if(!empty($parsedUrl[1])){
			$params = explode('&', $parsedUrl[1]);
		}
		$extraParams = explode('&', $this->extra_variables);
		$params = array_filter(
			array_merge($params, $extraParams)
		);
		
		if(!empty($params)){
			$url = $url . '?' . implode('&', $params);
		}

		return $url;
	}
	
	public function listHostOptions()
	{
		return CHtml::listData(Host::model()->findAll(), 'id_host', 'name');
	}
}